<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    function tap (ele, handler) {
      // 创建一个值，来判断是否可以执行对应的handler
      var 
        flag = true,
        time = 0,
        x = 0,
        y = 0
      ele.addEventListener('touchstart', function (e) {
        time = Date.now()
        flag = true
        x = e.changedTouches[0].clientX
        y = e.changedTouches[0].clientY
      })

      ele.addEventListener('touchmove', function (e) {
        var 
          moveX = e.changedTouches[0].clientX - x,
          moveY = e.changedTouches[0].clientY - y,
          move = Math.sqrt(Math.pow(moveX, 2) + Math.pow(moveY, 2))

        if (move > 6) { 
          flag = false
        }
      })

      ele.addEventListener('touchend', function (e) {
        if (flag && Date.now() - time <= 300) {
          handler.call(this, e)
        }
      })
    }

    tap(document, function () {
      console.log('tap执行')
    })
  
  </script>
</body>
</html>